class Solution {
public:
    int wiggleMaxLength(vector<int>& nums)
    {
        1
            int n = nums.size();
        vector<int> f(n, 1);
        auto g = f;
        int ret = 1;

        for (int i = 1; i < n; i++)
        {
            for (int j = 0; j < i; j++)
            {
                if (nums[j] > nums[i]) f[i] = max(f[i], g[j] + 1);
                if (nums[j] < nums[i]) g[i] = max(g[i], f[j] + 1);
            }
            ret = max(ret, max(f[i], g[i]));
        }
        return ret;
    }
};
class Solution {
public:
    int lengthOfLIS(vector<int>& nums)
    {
        int n = nums.size();
        vector<int> res;
        for (int i = 0; i < n; i++)
        {
            if (res.size() == 0 || nums[i] > res.back()) res.push_back(nums[i]);
            else
            {
                int left = 0, right = res.size() - 1;
                int mid;
                while (left < right)
                {
                    mid = left + (right - left) / 2;
                    if (res[mid] < nums[i]) left = mid + 1;
                    else right = mid;
                }
                res[left] = nums[i];
            }
        }
        return res.size();
    }
};
class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict)
    {
        int n = s.size();
        s = " " + s;
        vector<bool> dp(n + 1);
        dp[0] = true;
        unordered_set<string> hash;
        for (auto e : wordDict) hash.insert(e);
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= i; j++)
            {
                if (dp[j - 1] == true && hash.count(s.substr(j, i - j + 1)))
                {
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[n];
    }
};
class Solution {
public:
    int maxProduct(vector<int>& nums)
    {
        int n = nums.size();
        vector<int> f(n), g(n);
        int ret = nums[0];
        f[0] = g[0] = nums[0];
        for (int i = 1; i < n; i++)
        {
            f[i] = max(nums[i], max(f[i - 1] * nums[i], g[i - 1] * nums[i]));
            g[i] = min(nums[i], min(f[i - 1] * nums[i], g[i - 1] * nums[i]));
            ret = max(ret, f[i]);
        }
        return ret;
    }
};